﻿using System.ComponentModel;
using System.Web.Mvc;
using ShopB2C.Entity;
using ShopB2C.Helper;
using ShopB2C.Interfaces;

namespace ShopB2C.Controllers.Manager
{
    public class ModuleController : BaseController
    {
        private static IModule _module;

        public ModuleController()
        {
            this.PageSize = 15;
            _module = DataBaseExecuter.CreateClassByName<IModule>("Module");
        }

        #region Module

        public ActionResult ModuleList([DefaultValue(1)]int number)
        {
            int pageCount;
            ViewData["List"] = _module.GetClassModuleList(this.PageSize, number, out pageCount);
            ViewData["Pager"] = this.CreatePager(Url.Action("ModuleList", new { number = "{0}" }),
                pageCount, number);
            return View("ModuleList", new ModuleEntity() { PageNumber = number });
        }

        public ActionResult ModuleEdit([DefaultValue(1)]int number, [DefaultValue(0)]int id)
        {
            ModuleEntity cme = id == 0
                ? new ModuleEntity()
                : _module.GetClassModuleById(id);
            cme.PageNumber = number;
            return View(cme);
        }

        [HttpPost]
        public ActionResult ModuleEdit([DefaultValue(1)]int number, ModuleEntity cme)
        {
            if (cme.ModuleId == 0)
            {
                _module.ModuleAppend(cme);
            }
            else
            {
                _module.ModuleUpdate(cme);
            }
            return this.RedirectToAction("ModuleList", new { number = number });
        }

        public ActionResult ModuleDelete(string ids)
        {
            _module.ModuleDelete(ids);
            return RedirectToAction("ModuleList");
        }

        #endregion        
    }
}